﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace HQWeb.Procurement
{
    public partial class PurchaseInLibrary : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    BindMaterial(int.Parse(Request["id"]));
                }
                BindBranchName();
                applicant.Value = Session["USER_NAME"].ToString();
            }
        }

        #region 绑定分店列表
        private void BindBranchName()
        {
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            IList il = LibraryBll.GetModelList("");
            drop_branch.DataSource = il;
            drop_branch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            drop_branch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            drop_branch.DataBind();
            ListItem item = new ListItem("", "");
            item.Selected = true;
            drop_branch.Items.Add(item);
        }
        #endregion

        #region 绑定仓库
        private void BindLibraryName(int Branch_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            IList librarylist = bll.GetModelByBranch_id(Branch_id);
            if (librarylist != null)
            {
                droplibrary.DataSource = librarylist;
                droplibrary.DataTextField = "LIBRARY_NAME";
                droplibrary.DataValueField = "LIBRARY_ID";
                droplibrary.DataBind();
            }
        }
        #endregion

        protected void BindMaterial(int id)
        {
            Model.SysManage.T_PUB_MATERIAL model = new Model.SysManage.T_PUB_MATERIAL();
            BLL.SysManage.T_PUB_MATERIAL bll = new BLL.SysManage.T_PUB_MATERIAL();
            model = bll.GetModel(id);
            hidMaterialID.Value = model.MATERIAL_ID.ToString();
            Material_Name.Text = model.MATERIAL_NAME;
            hidTypeID.Value = model.TYPE_ID.ToString();
        }

        //protected void btnPass_Click(object sender, EventArgs e)
        //{
        //    try
        //    {        
        //    btnPass.Enabled = false;
        //    Model.Procurement.T_PUB_WAREHOUSING model = new Model.Procurement.T_PUB_WAREHOUSING();
        //    BLL.Procurement.T_PUB_WAREHOUSING bll = new BLL.Procurement.T_PUB_WAREHOUSING();
        //    model.APPLICANT = int.Parse(Session["USER_ID"].ToString());
        //    model.TIME = DateTime.Now;
        //    model.BRANCH_ID = int.Parse(drop_branch.SelectedValue);
        //    model.APPLICANT_NAME = Session["USER_NAME"].ToString();
        //    model.LIBRARY_ID = int.Parse(droplibrary.SelectedValue);
        //    model.MATERIAL_ID = int.Parse(hidMaterialID.Value);
        //    int lid = Convert.ToInt32(model.LIBRARY_ID);
        //    int mid = Convert.ToInt32(model.MATERIAL_ID);
        //    model.MATERIAL_NAME = Material_Name.Text;
        //    model.NUM = int.Parse(Materail_Num.Text);
        //    model.REMARK = Remark.Text.Trim();
        //    model.TITEL = applytitel.Value.Trim();
        //    Model.LibrarySys.T_PUB_LIBRARYMATERIAL lmmodel = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
        //    BLL.LibrarySys.T_PUB_LIBRARYMATERIAL lmbll = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
        //    lmmodel = lmbll.GetModel(lid, mid);
        //    if (lmmodel != null)
        //    {
        //        lmmodel.NUM += int.Parse(Materail_Num.Text);
        //        lmbll.AddPurchase(model, lmmodel);
        //        Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "采购入库" + model.MATERIAL_NAME + " 数量为" + model.NUM, DateTime.Now);
        //        Response.Redirect("InventoryList.aspx?pid="+ hidTypeID.Value);
        //       // Universal.JS.Alert("数据添加成功！");
        //    }
        //    else
        //    {
        //        Model.LibrarySys.T_PUB_LIBRARYMATERIAL addlmmodel = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
        //        addlmmodel.LIBRARY_ID = int.Parse(droplibrary.SelectedValue);
        //        addlmmodel.MATERIAL_ID = int.Parse(hidMaterialID.Value);
        //        addlmmodel.NUM = int.Parse(Materail_Num.Text);
        //        lmbll.EditPurchase(model, addlmmodel);
        //        Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "采购入库" + model.MATERIAL_NAME + " 数量为" + model.NUM, DateTime.Now);
        //        Response.Redirect("InventoryList.aspx?pid=" + hidTypeID.Value);
        //        //Universal.JS.Alert("数据添加成功！");
        //    }           
        //        //Universal.JS.Alert("数据添加成功！");
        //    }
        //    catch (Exception ex)
        //    {

        //        Universal.JS.Alert(ex.Message);
        //    }
        //        //btnPass.Enabled = false;
        //        //Universal.JS.Alert("数据填写有误！申请不成功！请您重新填写！");
        //}

        protected void drop_branch_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Branch_id = int.Parse(drop_branch.SelectedValue);
            BindLibraryName(Branch_id);
        }

        protected void btnReturnA_Click(object sender, EventArgs e)
        {
            Response.Redirect("InventoryList.aspx?pid=" + hidTypeID.Value);
        }
    }
}